package com.terrier.ui.effects
{
    import com.greensock.TweenLite;

    import flash.display.DisplayObject;
    import flash.filters.GlowFilter;

    public class WxGlowPulseEffect extends WxAbstractEffect
    {
        private static const INCREASE:int = 0;

        private static const DECREASE:int = 1;

        private var _alphaDim:Number = 0.0;

        private var _alphaBright:Number = 1.0;

        private var _glowColor:uint = 0xffff00;

        private var _glowFilter:GlowFilter = null;

        private var _glowBlur:Number = 18.0;

        private var _currentState:int = INCREASE;

        private var _updateStopFlag:Boolean = false;

        public function WxGlowPulseEffect(alphaDim:Number = 0.0, alphaBright:Number = 0.5, glowColor:uint = 0xffff00,
                                          glowBlur:Number = 18.0,
                                          target:DisplayObject = null, runTime:Number = 0.5,
                                          startEventParams:Object = null, updateEventParams:Object = null, stopEventParams:Object = null)
        {
            this.alphaDim = alphaDim;
            this.alphaBright = alphaBright;
            this.glowColor = glowColor;
            this.glowBlur = glowBlur;

            super(target, runTime, startEventParams, updateEventParams, stopEventParams);

            this._glowFilter = new GlowFilter(this.glowColor, this.alphaDim, this.glowBlur, this.glowBlur);
            this._updateStopFlag = false;
        }

        protected override function initialize():void
        {
            if (target != null)
            {
                this._glowFilter.alpha = alphaDim;
                target.filters = new Array(this._glowFilter);
            }
        }

        public override function start():void
        {
            super.start();
            if (target != null && this._glowFilter != null)
            {
                switch (this._currentState)
                {
                    case INCREASE:
                        TweenLite.to(this._glowFilter, runTime, { alpha: this.alphaBright,
                                         onUpdate: update, onComplete: reverse });
                        break;
                    case DECREASE:
                        TweenLite.to(this._glowFilter, runTime, { alpha: this.alphaDim,
                                         onUpdate: update, onComplete: reverse });
                        break;
                }
            }
        }

        public function update():void
        {
            if (target != null && this._glowFilter != null)
            {
                this.target.filters = new Array(this._glowFilter);
            }
        }

        public function reverse():void
        {
            if (target != null && this._glowFilter != null)
            {
                switch (this._currentState)
                {
                    case INCREASE:
                        this._currentState = DECREASE;
                        TweenLite.to(this._glowFilter, runTime,
                                     { alpha: this.alphaDim, onUpdate: update, onComplete: reverse });
                        break;
                    case DECREASE:
                        this._currentState = INCREASE;
                        TweenLite.to(this._glowFilter, runTime,
                                     { alpha: this.alphaBright, onUpdate: update, onComplete: reverse });
                        break;
                }
            }
        }

        public override function stop():void
        {
            super.stop();
            this.target.filters = null;
        }

        public override function clone():IWxAbstractEffect
        {
            var newEffect:WxGlowPulseEffect = new WxGlowPulseEffect(this.alphaDim, this.alphaBright,
                                                                    this.glowColor, this.glowBlur, this.target,
                                                                    this.runTime,
                                                                    this.startEventParams,
                                                                    this.updateEventParams,
                                                                    this.stopEventParams);
            return newEffect;
        }


        public function get alphaDim():Number
        {
            return _alphaDim;
        }

        public function set alphaDim(value:Number):void
        {
            _alphaDim = value;
        }

        public function get alphaBright():Number
        {
            return _alphaBright;
        }

        public function set alphaBright(value:Number):void
        {
            _alphaBright = value;
        }

        public function get glowColor():uint
        {
            return _glowColor;
        }

        public function set glowColor(value:uint):void
        {
            _glowColor = value;
        }

        public function get glowFilter():GlowFilter
        {
            return _glowFilter;
        }

        public function set glowFilter(value:GlowFilter):void
        {
            _glowFilter = value;
        }

        public function get glowBlur():Number
        {
            return _glowBlur;
        }

        public function set glowBlur(value:Number):void
        {
            _glowBlur = value;
        }

        public function get currentState():int
        {
            return _currentState;
        }

    }
}
